home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 21
/
Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso
/
Aminet
/
dev
/
e
/
Chris_emods.lha
/
execlists.e
< prev
next >
Wrap
Text File
|
1997-08-21
|
3KB
|
159 lines
-> buildlist.e - Example which uses an application-specific Exec list
OPT MODULE, REG = 5
MODULE 'exec/lists', 'exec/nodes', 'amigalib/lists'
CONST DATASIZE=62
OBJECT nameNode
ln:ln -> System Node structure
data[DATASIZE]:ARRAY -> Node-specific data
ENDOBJECT
CONST NAMENODE_ID=100 -> The type of 'nameNode'
-> Allocate a NameNode structure, copy the given name into the structure, then
-> add it the specified list. This example does not provide an error return
-> for the out of memory condition.
-> E-Note: ...instead it raises an exception which is handled by the caller
EXPORT PROC addName(list, name)
DEF namenode:PTR TO nameNode
NEW namenode
-> E-Note: copy *safely* to namenode.data
AstrCopy(namenode.data, name, DATASIZE)
namenode.ln.name := namenode.data
namenode.ln.type := NAMENODE_ID
namenode.ln.pri := 0
AddTail(list, namenode)
ENDPROC
EXPORT PROC freeNode(list:PTR TO lh, num)
DEF node:PTR TO ln, count, found
IF list.tailpred <> list
count := 0
node := list.head
found := FALSE
WHILE (found = FALSE) AND (node.succ)
IF count = num
found := TRUE
ELSE
node := node.succ
INC count
ENDIF
ENDWHILE
ENDIF
IF found
Remove(node)
IF node.succ
RETURN num
ELSE
RETURN num - 1
ENDIF
ENDIF
ENDPROC
-> Free the entire list, including the header. The header is not updated as
-> the list is freed. This function demonstrates how to avoid referencing
-> freed memory when deallocating nodes.
EXPORT PROC freeNameNodes(list:PTR TO lh)
DEF worknode:PTR TO nameNode, nextnode
worknode := list.head -> First node
WHILE nextnode := worknode.ln.succ
END worknode
worknode := nextnode
ENDWHILE
newList(list)
ENDPROC
EXPORT PROC findNodeNumber(list:PTR TO lh, str)
DEF node:PTR TO ln, count, found
IF list.tailpred <> list
count := 0
node := list.head
found := FALSE
WHILE (found = FALSE) AND (node.succ)
IF StrCmp(node.name, str)
found := TRUE
ELSE
node := node.succ
INC count
ENDIF
ENDWHILE
ENDIF
ENDPROC IF found THEN count ELSE 0
EXPORT PROC findNodeName(list:PTR TO lh, number)
DEF node:PTR TO ln, count
IF list.tailpred <> list
count := 0
node := list.head
WHILE node.succ AND (count < number)
node := node.succ
INC count
ENDWHILE
RETURN node.name
ENDIF
ENDPROC
EXPORT PROC movelistnode(l1:PTR TO lh, pos1, l2:PTR TO lh, pos2)
DEF pn:PTR TO ln, copynode = NIL:PTR TO lh, i
DEF forwards
IF (pos2 < 0) OR (pos2 >= countnodes(l2)) THEN RETURN pos1
IF (pos1 > pos2); forwards := FALSE
ELSEIF (pos1 < pos2); forwards := TRUE
ENDIF
IF l1 = l2
IF (pos1 = pos2) THEN RETURN pos1
IF forwards = FALSE THEN INC pos2
IF forwards THEN INC pos1
ENDIF
pn := l1
i := 0
WHILE (i < pos1) AND pn.succ
pn := pn.succ
INC i
ENDWHILE
copynode := pn
Remove(pn)
pn := l2
i := 0
WHILE (i < pos2) AND pn.succ
pn := pn.succ
INC i
ENDWHILE
Insert(l2, copynode, pn)
IF l1 = l2
IF forwards
RETURN i
ELSE
RETURN i - 1
ENDIF
ELSE
RETURN i
ENDIF
ENDPROC
EXPORT PROC countnodes(list:PTR TO lh)
DEF node:PTR TO ln, count
count := 0
IF list.tailpred <> list
node := list.head
WHILE (node.succ)
node := node.succ
INC count
ENDWHILE
ENDIF
ENDPROC count